package com.wxgo.dao;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.wxgo.common.model.News;
import com.wxgo.dao.base.BaseDao;

public class NewsDao extends BaseDao {

	public static final NewsDao me = new NewsDao();

	public List<News> find(String where, Object... paras) {
		String sql = "select n.id,newsname,site,province,type,remark,vipprice,intype from news n left join outtype o on o.id=n.outtype ";
		if (where != null && where.length() > 0) {
			sql += where;
		}
		if (paras != null && paras.length > 0) {
			return News.dao.find(sql, paras);
		} else {
			return News.dao.find(sql);
		}
	}

	public long count(String where, Object... paras) {
		String sql = "select count(*) from news ";
		if (where != null && where.length() > 0) {
			sql += where;
		}
		long count = 0;
		if (paras != null && paras.length > 0) {
			count = Db.queryLong(sql, paras);
		} else {
			count = Db.queryLong(sql);
		}
		return count;
	}

	public News findById(Integer id) {
		return News.dao.findById(id);
	}

	public List<News> find(String sql) {
		return News.dao.find(sql);
	}

	public Page<News> paginate(int pageNumber, int pageSize, String select, String sqlExceptSelect) {
		return News.dao.paginate(pageNumber, pageSize, select, sqlExceptSelect);
	}

	public boolean truncate() {
		int num = Db.update("truncate table news");
		return num >= 0;
	}

	public boolean saveList(List<News> list) {
		int[] nums = Db.batchSave(list, list.size());
		return nums.length >= 0;
	}

	public boolean save(News news) {
		return news.save();
	}

	public boolean updatePcode() {
		int num = Db.update("update news n left join province p on p.province=n.province set n.pcode=p.id");
		return num >= 0;
	}

}
